package com.huiw.archives.modules.sys.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.huiw.archives.common.utils.R;
import com.huiw.archives.common.utils.database.PageParams;
import com.huiw.archives.common.utils.database.PageUtils;
import com.huiw.archives.common.utils.gson.PerfectGson;
import com.huiw.archives.modules.sys.entity.WisArchivesTask;

/**
 * 
 * 
 * @author yangcheng
 * @email yangcheng@huiwtech.com
 * @date 2017-11-17 10:51:53
 */
@RestController
@RequestMapping("wisarchivestask")
public class WisArchivesTaskController extends BaseControl {

	/**
	 * 条件列表
	 * 
	 * @param taskStatus
	 *            任务状态
	 * @throws Exception
	 */
	@RequestMapping("/list/{taskStatus}")
	public R list(@RequestParam Map<String, Object> params, @PathVariable("taskStatus") Integer taskStatus)
			throws Exception {
		// 查询列表数据
		PageParams query = new PageParams(params);
		// 查询条件
		Map<String, Object> whereParams = new HashMap<String, Object>();
		whereParams.put("taskStatus", taskStatus);
		// 获取完美gson
		Gson gson = PerfectGson.getGson();

		List<Object> wisArchivesTaskList = wisArchivesTaskService.query(new WisArchivesTask(), query, whereParams);
		// json转为带泛型的list
		List<WisArchivesTask> reList = gson.fromJson(gson.toJson(wisArchivesTaskList),
				new TypeToken<List<WisArchivesTask>>() {
				}.getType());
		int total = (int) wisArchivesTaskService.queryRecordNum(new WisArchivesTask(), "", "");

		PageUtils pageUtil = new PageUtils(reList, total, query.getLimit(), query.getPage());

		return R.ok().put("page", pageUtil);
	}

	/**
	 * 列表
	 * 
	 * @throws Exception
	 */
	@RequestMapping("/list")
	public R list(@RequestParam Map<String, Object> params) throws Exception {
		// 查询列表数据
		PageParams query = new PageParams(params);
		// 获取完美gson
		Gson gson = PerfectGson.getGson();

		List<Object> wisArchivesTaskList = wisArchivesTaskService.query(new WisArchivesTask(), query);
		// json转为带泛型的list
		List<WisArchivesTask> reList = gson.fromJson(gson.toJson(wisArchivesTaskList),
				new TypeToken<List<WisArchivesTask>>() {
				}.getType());
		int total = (int) wisArchivesTaskService.queryRecordNum(new WisArchivesTask(), "", "");

		PageUtils pageUtil = new PageUtils(reList, total, query.getLimit(), query.getPage());

		return R.ok().put("page", pageUtil);
	}

	/**
	 * 信息
	 * 
	 * @throws Exception
	 */
	@RequestMapping("/info/{pid}")
	public R info(@PathVariable("pid") String pid) throws Exception {
		// 获取完美gson
		Gson gson = PerfectGson.getGson();
		// json转为带泛型的list
		WisArchivesTask wisArchivesTask = gson.fromJson(
				gson.toJson(wisArchivesTaskService.query(pid, new WisArchivesTask())),
				new TypeToken<WisArchivesTask>() {
				}.getType());
		return R.ok().put("wisArchivesTask", wisArchivesTask);
	}

	/**
	 * 保存
	 */
	@RequestMapping("/save")
	public R save(@RequestBody WisArchivesTask wisArchivesTask) throws Exception {
		wisArchivesTaskService.save(wisArchivesTask);

		return R.ok();
	}

	/**
	 * 修改
	 */
	@RequestMapping("/update")
	public R update(@RequestBody WisArchivesTask wisArchivesTask) throws Exception {
		wisArchivesTaskService.update(wisArchivesTask);

		return R.ok();
	}

	/**
	 * 删除
	 */
	@RequestMapping("/delete")
	public R delete(@RequestBody String[] pids) throws Exception {
		wisArchivesTaskService.deletebatch(new WisArchivesTask(), pids);

		return R.ok();
	}

}
